package com.wxsh.english;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wxsh.entity.VocTb;
import com.wxsh.service.VocTbService;

/**
 * 单词风暴(http://www.wordstorming.com/)导出的MP3单词释义显示不全问题修复
 * 1. 单词增加英美音标
 * 2. 换行开头增加时间轴以修复Lyric显示不全问题
 *
 */
public class WordPhonetic {

	private static Logger logger = LoggerFactory.getLogger(WordPhonetic.class);
	private static ApplicationContext context;
	private static VocTbService vocTbService;
	
	static{
		context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		vocTbService = (VocTbService) context.getBean("vocTbServiceImpl");
	}

	public static void main(String[] args) throws Exception {
		String preName = "OxfordWords";
		int total = 5;
		for(int i=1; i<=total; i++) {
			String name = preName+".lrc";
			if(total>1) {
				name = preName+i+".lrc";
			}
			addPhonetic(name);
			fixLrc(name);
		}
		
	}
	
	/**
	 * 1. 添加音标
	 * @param name
	 * @throws Exception
	 */
	public static void addPhonetic(String name) throws Exception {
		File file = new File("D:\\Programs\\ActiveStorm\\WordStorming\\MyMP3s\\"+name);
		File dest = new File("D:\\Programs\\ActiveStorm\\WordStorming\\MyMP3s\\lrc\\"+name);
		
		List<String> destLines = new ArrayList<String>();
		List<String> lines = FileUtils.readLines(file, "GB2312");
		for (int i=0; i<lines.size(); i++) {
			String line = lines.get(i);
			line = line.trim();
			while(line.contains("  ")) {
				line = line.replaceAll("  ", " ");
			}
			if(line.matches("^\\[\\d\\d+:\\d\\d\\.\\d\\d\\].*")) {
				int start = line.indexOf("]");
				int end = line.indexOf("/");
				String word = line.substring(start+1, end).trim();
				logger.debug(word);
				if(word.matches("^[A-Za-z]{1,}")) {
					String phonetic = "";
					VocTb vocTb = processPhonetic(word);
					if(vocTb == null){
						logger.error("数据库不存在此单词:{}", word);
						continue;
					}
					String phoneticBr = vocTb.getVcPhoneticUk();
					String phoneticAm = vocTb.getVcPhoneticUs();
					if(StringUtils.isBlank(phoneticBr)&&StringUtils.isNotBlank(phoneticAm)){
						phonetic = "美/英"+phoneticAm;
					}else if(StringUtils.isNotBlank(phoneticBr)&&StringUtils.isBlank(phoneticAm)) {
						phonetic = "英/美"+phoneticBr;
					}else if(StringUtils.isNotBlank(phoneticBr)&&StringUtils.isNotBlank(phoneticAm)) {
						phonetic = "英"+phoneticBr+"美"+phoneticAm;
					}
					if(StringUtils.isBlank(phonetic)) {
						destLines.add(insertChars(line, end, "[#$#]"));
					}else {
						destLines.add(insertChars(line, end, phonetic));
					}
				}else {
					destLines.add(line);
				}
			}else {
				destLines.add(line);
			}
		}
		
		FileUtils.writeLines(dest, "UTF-8", destLines, false);
	}

	//1.1 本地查询音标
	public static VocTb processPhonetic(String word) {
		List<VocTb> vocTbs = vocTbService.findByWord(word);
		for (VocTb vocTb : vocTbs) {
			if(word.equals(vocTb.getVcVocabulary())){
				return vocTb;
			}
		}
		if(vocTbs.size()>0){
			logger.info("原单词:{}", word);
			logger.info("查到单词:{}", vocTbs.get(0).getVcVocabulary());
			return vocTbs.get(0);
		}
		return null;
	}
	
	//1.2 插入音标
	public static String insertChars(String source, int index, String chars) {
		return source.substring(0, index) + chars + source.substring(index);
	}
	
	//2. 换行开头增加时间轴以修复Lyric显示不全问题
	public static void fixLrc(String name) throws Exception{
		List<String> lines = FileUtils.readLines(new File("D:\\Programs\\ActiveStorm\\WordStorming\\MyMP3s\\lrc\\"+name), "UTF-8");
		File dest = new File("D:\\Programs\\ActiveStorm\\WordStorming\\MyMP3s\\lrcfixed\\"+name);
		List<String> destLines = new ArrayList<String>();
		List<String> tmpLines = new ArrayList<String>();
		int i=1;
		for (String line : lines) {
			if(line.startsWith("[00:00.00]") && i<10){
				line = line.replace("[00:00.00]", "[00:02.00]");
				tmpLines.add("[00:00.00]");
				tmpLines.add("[00:01.00]");
				tmpLines.add(line);
			}else{
				tmpLines.add(line);
			}
			i++;
		}
		
		String tempTime = "";
		for (String line : tmpLines) {
			if(line.matches("^\\[\\d\\d+:\\d\\d\\.\\d\\d\\].*")) {
				int endIdx = line.indexOf("]")+1;
				tempTime = line.substring(0, endIdx);
				destLines.add(line);
			}else {
				destLines.add(tempTime+line);
			}
		}
		
		FileUtils.writeLines(dest, "UTF-8", destLines, false);
	}
	
}
